package com.tedu.charging.common.mq.rabbitmq;

import com.tedu.charging.common.mq.AbstractProducer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitmqProducerAdapter extends AbstractProducer {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public boolean sendMessage(String exchangeName, String routingKey, Object message) {
        try {
            rabbitTemplate.convertAndSend(exchangeName,routingKey,message);
            handleSuccess(message);
            return  true;
        }catch (Throwable e){
            handleError(message,e);
            return  false;
        }
    }

    @Override
    public boolean sendMessage(Object message) {
        try {
            rabbitTemplate.convertAndSend(message);
            handleSuccess(message);
            return  true;
        }catch (Throwable e){
            handleError(message,e);
            return  false;
        }
    }

    @Override
    public boolean sendMessage(String routingKey,Object message) {
        try {
            rabbitTemplate.convertAndSend(routingKey,message);
            handleSuccess(message);
            return  true;
        }catch (Throwable e){
            handleError(message,e);
            return  false;
        }

    }
}
